package com.xjprojectc.base.action;

import java.util.List;

import net.sf.json.JSONObject;

import com.xjprojectc.business.pojo.T_agentaccountinfo;
import com.xjprojectc.business.service.AgentaccountinfoService;
import com.xjprojectc.uitl.JsonUtil;
import com.xjprojectc.webservice.Call;


/**
 * 
 * 
 * 用户登录管理
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class LoginAction extends SysBaseAction {
	private AgentaccountinfoService agentaccountinfoService;
	private T_agentaccountinfo  user;
	private String code;
	private String errorMsg;
	/**
	 * 
	 * 用户登录
	 * 
	 * @return
	 * @throws Exception 
	 */
	@SuppressWarnings({ "unchecked", "static-access" })
	public String userLogin(){
		String result = null;
		if(user == null){
			return "logininit";
		}
		JSONObject jsonobj = new JSONObject();
		user.setClientip(this.getClientIP());
		user.setClientmac(this.getClientMac());
		String conText = jsonobj.fromObject(user).toString();
		try {
			result = Call.getCall().exAgentLogin(conText);
		} catch (Exception e) {
			errorMsg = "远程服务器连接异常！";
			return "logininit";
		}
		jsonobj = jsonobj.fromObject(result);
		if(jsonobj.get("success").equals("false")){
			errorMsg = jsonobj.get("data").toString();
			return "logininit";
		}
		T_agentaccountinfo user = (T_agentaccountinfo) JsonUtil.getObject4JsonString(jsonobj.get("data").toString(), T_agentaccountinfo.class);
		if(user.getShenhe() ==0 ){
			errorMsg = "帐号审核中！";
			return "logininit";
		}else if(user.getShenhe() == 2){
			errorMsg = "帐号审核未通过！";
			return "logininit";
		}
		this.getReqeust().getSession().setAttribute("sysUser", user);
		
		
		T_agentaccountinfo agent = new T_agentaccountinfo();
		agent.setUsername(user.getUsername());
		agent.setPassword(user.getPassword());
		List<T_agentaccountinfo> agentList = agentaccountinfoService.getAgentaccountinfoByList(agent);
		user.setId(agentList.get(0).getId());
		user.setField2(null);
		user.setField3(null);
		user.setAccountmoney(null);
		agentaccountinfoService.updateAgentaccountinfo(user);
		return "main";
		
	}
	
	/**
	 * 
	 * 用户退出
	 * @return
	 */
	public String loginOut(){
		this.getReqeust().getSession().removeAttribute("sysUser");
		return "logininit";
	}
	
	public String setLanguage(){
		this.getReqeust().getSession().setAttribute("language", code);
		return NONE;
	}

	public T_agentaccountinfo getUser() {
		return user;
	}

	public void setUser(T_agentaccountinfo user) {
		this.user = user;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public AgentaccountinfoService getAgentaccountinfoService() {
		return agentaccountinfoService;
	}

	public void setAgentaccountinfoService(
			AgentaccountinfoService agentaccountinfoService) {
		this.agentaccountinfoService = agentaccountinfoService;
	}
	
}
